import { _decorator, Component, Node, Sprite, Label, ImageAsset, SpriteFrame } from 'cc';
import { DataManager } from '../Public/DataManager';
const { ccclass, property } = _decorator;

@ccclass('Message')
export class Message extends Component {
    @property(Sprite)
    head:Sprite=null;
    @property(Label)
    userName:Label=null;
    @property(Label)
    level:Label=null;
    @property(Node)
    area:Node=null;
    @property(Label)
    id:Label=null;
    @property(Label)
    createDate:Label=null;
    start() {
        this.initData();
    }

    update(deltaTime: number) {

    }
    /**返回主页 */
    BacktoMain() {
        this.node.setPosition(600,0);
    }
    /**初始化数据 */
    initData()
    {
        const {head_portrait,name,nodeLens,game_user_id,create_time}=DataManager.Instance
        const image = new Image();
        image.src =head_portrait;
        image.onload = () => {
            const l = new ImageAsset(image);
            const sp = SpriteFrame.createWithImage(l);
            this.head.spriteFrame=sp;
            console.log('微信头像加载成功',)
        }
        this.userName.string=name;
        this.level.string=`挑战关卡:第${nodeLens+1}关卡`;
        this.id.string=`id:${game_user_id}`;
        const res=create_time.match(/^\d+-\d+-\d+/)
        if(res)
        {
            this.createDate.string=`注册日期 ${res[0]}`;
        }
        else
        {
            this.createDate.string='获取注册日期失败';
        }
    }
    /**打开收获地址 */
    openArea()
    {
        this.area.setPosition(0,0);
        this.node.setPosition(600,0);
    }
}

